/**
 * Created by hwen on 15/8/18.
 */

var goodses,goodsesLenght;
var pageSize,pageNum,page; // pageNum--页数,pageSize--每页显示数目,page--当前页码

function addRecord(tr){
    var type        = $(".type",tr).text(); //不为空
    var goodsName   = $(".goodsName",tr).text(); //不为空
    var description = $(".description",tr).text();
    var price       = $(".price",tr).text();
    var count       = $(".count",tr).text();
    var weight      = $(".weight",tr).text();

    var record = {"type":type,"goodsName":goodsName,"description":description,"price":price,
        "count":count,"weight":weight};

    //var jsonData = JSON.stringify(data);

    console.log(record);

    //作用同$.post();
    //$.ajax({
    //    url:"/goods",
    //    type:"post",
    //    data:data,
    //    success: function (res) {
    //        alert(res);
    //    }
    //});

    $.post("/goods",record,function(data,status){
        console.log("goods add..../ndata= "+ data+ "/n Status = "+ status);
        $(".id",tr).text(data);
        goodsesLenght++;
        var newPageNum=Math.ceil(goodsesLenght/pageSize);
        if(pageNum<newPageNum){
            pageNum=newPage;
            $(".pagination li:nth-last-of-type(2)").before("<li id='page'><a id='"+pageNum+"'>"+pageNum+"</a></li>");
        }
    });
}

function showRecord(tb,tr){
    $.get("/goods?json",function(data,status){
        goodses = $(data);
        page= $(".active","li").attr("id");
        pageSize= $("#pageSize").find('option:selected').text();
        // or $("#pageSize option:selected").text();
        //    $("#pageSize").val(); auto get selected's val but not text

        goodsesLenght=0;
        goodses.each(function(index,goods){
            goodsesLenght++;
            if(index>(pageSize-1)) {return true;} // continue;
            console.log(goods.id);
            var newTr = tr.clone();
            $(".id",newTr).text(goods.id);
            $(".type",newTr).text(goods.type);
            $(".goodsName",newTr).text(goods.goodsName);
            $(".description",newTr).text(goods.description);
            $(".price",newTr).text(goods.price);
            $(".count",newTr).text(goods.count);
            $(".weight",newTr).text(goods.weight);

            $("#mainTbody",tb).append(newTr);

            $("#saveAdd",newTr).hide();
            $("#reset",newTr).hide();
            $("#deleteItem2",newTr).show();

            $("#saveEdit",newTr).hide();
            $("#cancal",newTr).hide();

            $("#editItem2",newTr).show();
        });

        pageNum=Math.ceil(goodsesLenght/pageSize); //向上取整, Math.floor():向下,Math.round():四舍五入
        console.log("page size->"+pageNum+" lenght->"+goodsesLenght+" pageSize->"+pageSize);
        $(".pagination li:nth-last-of-type(2)").before("<li id='page' class='active'><a id='"+1+"'>"+1+"</a></li>");
        for(var i=2;i<=pageNum;i++){
            $(".pagination li:nth-last-of-type(2)").before("<li id='page'><a id='"+i+"'>"+i+"</a></li>");
        }
    });
}

function jump(){
    goodses = $(data);
    page= $(".active","li").attr("id");
    pageSize= $("#pageSize").find('option:selected').text();
    // or $("#pageSize option:selected").text();
    //    $("#pageSize").val(); auto get selected's val but not text

    $.get("/goods?json",function(data,status){

        goodsesLenght=0;
        goodses.each(function(index,goods){
            goodsesLenght++;
            if(index>(pageSize-1)) {return true;} // continue;
            console.log(goods.id);
            var newTr = tr.clone();
            $(".id",newTr).text(goods.id);
            $(".type",newTr).text(goods.type);
            $(".goodsName",newTr).text(goods.goodsName);
            $(".description",newTr).text(goods.description);
            $(".price",newTr).text(goods.price);
            $(".count",newTr).text(goods.count);
            $(".weight",newTr).text(goods.weight);

            $("#mainTbody",tb).append(newTr);

            $("#saveAdd",newTr).hide();
            $("#reset",newTr).hide();
            $("#deleteItem2",newTr).show();

            $("#saveEdit",newTr).hide();
            $("#cancal",newTr).hide();

            $("#editItem2",newTr).show();
        });
    });
}
